package com.liancheng.smarthome.module.UpdatePwd;

import android.content.Intent;

import com.httpapi.QHttpApi; 
import com.httpapi.apiservice.OnHttpApiListener;

public class UpdatePwdVM extends BaseVM<UpdatePwdActivity> implements OnHttpApiListener {

    public void changePWD(String oldPwd,String newPwd){
        LogTag.d("oldPwd-->"+oldPwd);
        LogTag.d("newPwd-->"+newPwd);
        //从本地获取登陆的用户 id
        Integer employeeId = LoginBean.getUserId();
        Map<String, Object> params = new HashMap<>();
        params.put("employeeId",employeeId);
        params.put("oldPassword",oldPwd);
        params.put("employeePassword",newPwd);
        // 第三个参数，需要用到哪个类解析数据结果就传哪个类进去就行，这里采用了泛型解析
        String url = HttpManager.updatePwdUrl;
        showLoading();
        UpdatePwdVM.this.subscriber=
                QHttpApi.doJsonPUT(url, params, LoginBean.class, HttpManager.updatePwdTag, UpdatePwdVM.this);
    }

    @Override
    public void onSuccess(int what, Object data) {
        LogTag.d("data-->"+data);
        dissmissLoading();
        switch (what){
            case HttpManager.updatePwdTag:
                if(data == null) return;
                int value = new BigDecimal(data.toString()).intValue();
                if(value>0){
                    ToastUtil.showShort("修改成功");
                    LoginBean.loginOutAndClearEmployee();
                    AppManager.finishAll();
                    context.startActivity(new Intent(context, SplashActivity.class));
                }else{
                    ToastUtil.showShort("修改失败，请重试");
                }
                break;
                default:
                    break;
        }
    }

    @Override
    public void onFailure(int what, String msg, int code) {
        showFailMsg(msg);
    }
}
